using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml;
using System.Web;
using System.IO;
using System.IO.IsolatedStorage;

namespace SharePointSearchBench
{
    /// <summary>
    /// Interaction logic for QueryPacketSetup.xaml
    /// </summary>
    public partial class QueryPacketSetup : System.Windows.Window
    {

        QueryType queryType;
        bool isDirty;

        /// <summary>
        /// Creates a new instance of the class.
        /// </summary>
        /// <param name="queryType"></param>
        public QueryPacketSetup(QueryType queryType)
        {
            InitializeComponent();
            this.queryType = queryType;
            this.InitializePacketText();
        }

        void PacketTextChanged(object sender, EventArgs e)
        {
            isDirty = true;
        }

        void InitializePacketText()
        {
            if(this.queryType == QueryType.FullText)
            {
                this.packetTypeTextBlock.Text = "You are modifying the FULL TEXT query packet.";

                FullTextQueryPacket packet = new FullTextQueryPacket();
                string packetValue = packet.Value;
                if(string.IsNullOrEmpty(packetValue))
                {
                    this.packetValueTextBox.Text = ConfigSettings.Instance.FullTextQueryPacket;
                }
                else
                {
                    this.packetValueTextBox.Text = packetValue;
                }
            }
            else
            {
                this.packetTypeTextBlock.Text = "You are modifying the KEYWORD query packet.";
                KeywordQueryPacket packet = new KeywordQueryPacket();
                string packetValue = packet.Value;
                if(string.IsNullOrEmpty(packetValue))
                {
                    this.packetValueTextBox.Text = ConfigSettings.Instance.KeywordQueryPacket;
                }
                else
                {
                    this.packetValueTextBox.Text = packetValue;
                }
            }


        }

        void SavePacketText()
        {
            if(this.queryType == QueryType.FullText)
            {
                FullTextQueryPacket packet = new FullTextQueryPacket();
                packet.Value = packetValueTextBox.Text;
                packet.Save();

            }
            else
            {
                KeywordQueryPacket packet = new KeywordQueryPacket();
                packet.Value = packetValueTextBox.Text;
                packet.Save(); 
            }

        }

        void OkButtonClick(object sender, EventArgs e)
        {
            this.DialogResult = true;
        }

        void CancelButtonClick(object sender, EventArgs e)
        {
            this.DialogResult = false;
        }

        void RevertButtonClick(object sender, EventArgs e)
        {
            string packetText;

            if(queryType == QueryType.Keyword)
            {
                packetText = ConfigSettings.Instance.KeywordQueryPacket;
            }
            else
            {
                packetText = ConfigSettings.Instance.FullTextQueryPacket;
            }

            this.packetValueTextBox.Text = packetText;
            
        }

        protected override void OnClosed(EventArgs e)
        {
            if(isDirty)
            {
                SavePacketText();
            }

            base.OnClosed(e);
        }
    }
}